package com.hempton.flexmarkers;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;

public class FlexMarkerManager {
	
	public static final String MARKER_TYPE = "com.hempton.flexmarkers.flexProblem";
	
	public void removeMarkers(IResource resource) {
		try {
			for(IMarker marker : resource.findMarkers(FlexMarkerManager.MARKER_TYPE, true, IResource.DEPTH_INFINITE)) {
				marker.delete();
			}
		} catch (CoreException e) {
			// TODO Look into logging
			e.printStackTrace();
		}
	}
	
	public void addMarker(IFile file, Integer lineNumber, Integer columnNumber, boolean isError, String message) {
		
		if(file == null) {
			// TODO: put a marker on the project since to file is available
			return;
		}
		
		try {
			IMarker marker = file.createMarker(MARKER_TYPE);
			marker.setAttribute(IMarker.SEVERITY, isError ? IMarker.SEVERITY_ERROR : IMarker.SEVERITY_WARNING);
			marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
			marker.setAttribute(IMarker.MESSAGE, message);
		} catch (CoreException e) {
			// TODO: look into logging
			e.printStackTrace();
		}
	}

}
